//
//  OYCNewFeatureController.swift
//  WeiBo-Swift
//
//  Created by daniel on 2017/8/9.
//  Copyright © 2017年 daniel. All rights reserved.
//

import UIKit
import SnapKit

private let reuseIdentifier = "Cell"

class OYCNewFeatureController: UICollectionViewController {
    
    let imageDatas = ["new_feature_1","new_feature_2","new_feature_3","new_feature_4"]
    
    init() {
        let layout = UICollectionViewFlowLayout.init()
        let screenW = UIScreen.main.bounds.size.width
        let screenH = UIScreen.main.bounds.size.height
        
        layout.itemSize = CGSize.init(width: screenW, height: screenH)
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = .horizontal
        
        super.init(collectionViewLayout: layout)
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.isPagingEnabled = true
        collectionView?.bounces = false
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        self.collectionView!.register(OYCNewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using [segue destinationViewController].
        // Pass the selected object to the new view controller.
    }
    */

    // MARK: UICollectionViewDataSourc

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return 4
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! OYCNewFeatureCell
        
        cell.model = imageDatas[indexPath.item]
        
        cell.startBtn.isHidden = true
        
    
        return cell
    }
    
    override var prefersStatusBarHidden: Bool {
        return true
    }
    
    override func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        let index = Int(scrollView.contentOffset.x / UIScreen.main.bounds.size.width)
        if index == (imageDatas.count - 1) {
            let collection = scrollView as! UICollectionView
            let cell = collection.cellForItem(at: IndexPath.init(row: index, section: 0)) as! OYCNewFeatureCell
            cell.startBtn.transform = CGAffineTransform.init(scaleX: 0, y: 0)
            cell.startBtn.isHidden = false
            
            UIView.animate(withDuration: 1.0,
                           delay: 0,
                           usingSpringWithDamping: 0.5,
                           initialSpringVelocity: 0,
                           options: UIViewAnimationOptions.curveEaseInOut,
                           animations: { 
                            
                            cell.startBtn.transform = CGAffineTransform.init(scaleX: 1.3, y: 1.3)
                            
            }, completion: { (isFinish) in
                if isFinish {
                    cell.transform = CGAffineTransform.identity
                }
            })
        }
    }
    
    // MARK: UICollectionViewDelegate

    /*
    // Uncomment this method to specify if the specified item should be highlighted during tracking
    override func collectionView(_ collectionView: UICollectionView, shouldHighlightItemAt indexPath: IndexPath) -> Bool {
        return true
    }
    */

    /*
    // Uncomment this method to specify if the specified item should be selected
    override func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {
        return true
    }
    */

    /*
    // Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
    override func collectionView(_ collectionView: UICollectionView, shouldShowMenuForItemAt indexPath: IndexPath) -> Bool {
        return false
    }

    override func collectionView(_ collectionView: UICollectionView, canPerformAction action: Selector, forItemAt indexPath: IndexPath, withSender sender: Any?) -> Bool {
        return false
    }

    override func collectionView(_ collectionView: UICollectionView, performAction action: Selector, forItemAt indexPath: IndexPath, withSender sender: Any?) {
    
    }
    */

}

private class OYCNewFeatureCell: UICollectionViewCell {
    
    lazy var imageView: UIImageView = {
        let screenW = UIScreen.main.bounds.size.width
        let screenH = UIScreen.main.bounds.size.height
        let imageV = UIImageView.init(frame: CGRect.init(x: 0, y: 0, width: screenW, height: screenH))
        
        return imageV
    }()
    
    lazy var startBtn: UIButton = {
        let btn = UIButton.init(type: UIButtonType.custom)
        
        btn.setBackgroundImage(UIImage.init(named: "new_feature_finish_button_highlighted") , for: UIControlState.highlighted)
        btn.setBackgroundImage(UIImage.init(named:"new_feature_finish_button"), for: UIControlState.normal)
        btn.setTitle("开始体验", for: UIControlState.normal)
        btn.sizeToFit()
        btn.isHidden = true
        btn.addTarget(self, action: #selector(OYCNewFeatureCell.startBtnClick), for: .touchUpInside)
        
        return btn
    }()
    
    @objc private func startBtnClick() {
        print(#function)
    }

    
    var model: String? {
        
        didSet {
            imageView.image = UIImage.init(named: model!)
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        contentView.addSubview(imageView)
        contentView.addSubview(startBtn)
        startBtn.snp.makeConstraints({ (make) in
            make.centerX.equalTo(self.contentView)
            make.bottom.equalTo(self.contentView).multipliedBy(0.7)
        })
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
